package pers.vinsonws.jianghu.clientwin.ui;

import pers.vinsonws.jianghu.clientwin.global.Info;
import pers.vinsonws.jianghu.clientwin.model.ChatRoomClient;
import pers.vinsonws.jianghu.clientwin.model.UserClient;
import pers.vinsonws.jianghu.util.ChatRoom;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;

/**
 * @auther vinsonws
 * @date 2018/11/17 23:27
 */

public class OnlineUserUi extends JFrame {
    private static final long serialVersionUID = 1L;
    private JPanel onlineUserList;
    private HashMap<Integer, JButton> jButtonHashMap;

    private final JTabbedPane tabbedPane;

    public OnlineUserUi() {
        jButtonHashMap = new HashMap<>();
        //setLocation(220, 120);              //
        tabbedPane = Info.tabbedPane;

        //用户信息



        //在线列表
        onlineUserList = new JPanel();
        onlineUserList.setBackground(Color.white);
        tabbedPane.addTab("在线用户列表", onlineUserList);



    }


    public void paintOnlineUser(){
        jButtonHashMap.clear();
//        for (int i=0;i<chatRoomList.getComponentCount();i++){
//            chatRoomList.remove(i);
//
//        }
        onlineUserList.removeAll();
        for (UserClient userClient: Info.userClientHashMap.values()){
            createUserButton(userClient.getId(), userClient.getName());
        }
        onlineUserList.repaint();
        onlineUserList.revalidate();
    }
    private void createUserButton(Integer userId, String userName){
        if (userId == Info.id) return;
        JButton createUser= new JButton(userName);
        Info.initStatus = false;
        createUser.setFont(new Font("宋体", Font.CENTER_BASELINE,22));
        int size ;
        if (jButtonHashMap != null) size = jButtonHashMap.size();
        else size = 0;
        createUser.setBounds(jButtonHashMap.size() + (size % 5) * 200, jButtonHashMap.size() + (size / 5 ) * 200,
                200, 200);
        createUser.setContentAreaFilled(true);
        createUser.setBackground(Color.cyan);
        createUser.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (Info.userOpened.get(userId) == null){
                    ChatOneUi chatOneUi = new ChatOneUi();
                    chatOneUi.openChatOneWin(userId, userName);
                    Info.userOpened.put(userId, chatOneUi);
                }
            }
        });

        onlineUserList.add(createUser);

        jButtonHashMap.put(userId, createUser);
    }

    public JTabbedPane getTabbedPane() {
        return tabbedPane;
    }
}
